/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ViewAgegroupDlg.java
 *
 * Created on May 5, 2010, 8:32:06 AM
 */
package com.aptech.childcare.gui;

import com.aptech.childcare.buz.ChildcareBuz;
import com.aptech.childcare.components.ColumnData;
import com.aptech.childcare.components.ObjectTableModel;
import com.aptech.childcare.entities.Agegroup;
import java.awt.Image;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 *
 * @author BaoTien
 */
public class ViewAgegroupDlg extends java.awt.Dialog {

    private ChildcareBuz buz = new ChildcareBuz();
    private ObjectTableModel tableModel;
    private ArrayList<Agegroup> age = new ArrayList<Agegroup>();
    int confirm = 0, id = 0;
    // Table's headers
    private JTable headerTable;
    // Index of the selected row
    int index = -1;
    public static int AgeID;
    public static int AgeID1;
    MainFrm frm;

    /** Creates new form ViewAgegroupDlg */
    public ViewAgegroupDlg(MainFrm frm, boolean modal) throws IOException {
        super(frm, modal);
        this.frm = frm;
        ImageIcon icon = new ImageIcon(getClass().getResource("../icon/logo.png"));
        Image image = icon.getImage();
        setIconImage(image);
        try {
            UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
            SwingUtilities.updateComponentTreeUI(this);
        } catch (UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        }
        initComponents();
        loadtableAgegroup();
        setLocationRelativeTo(null);
    }

    /**
     *
     */
    public void loadtableAgegroup() throws IOException {
        age = buz.findAgegroup();
        ColumnData[] columns = {
            new ColumnData("ID", 20, SwingConstants.LEFT, 1),
            new ColumnData("Age Group", 100, SwingConstants.LEFT, 2),
            new ColumnData("Fees", 20, SwingConstants.LEFT, 3),
            new ColumnData("Note", 100, SwingConstants.LEFT, 4),};
        tableModel = new ObjectTableModel(tbAgegroup, columns, age);
        headerTable = tableModel.getHeaderTable();

        // Create numbering column
        headerTable.createDefaultColumnsFromModel();
        tbAgegroup.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        // Put it in a viewport that we can control a bit
        JViewport viewport = new JViewport();

        // Display numbering column
        viewport.setView(headerTable);
        viewport.setPreferredSize(headerTable.getMaximumSize());

        scrAgegroup.setRowHeader(viewport);
        scrAgegroup.setCorner(ScrollPaneConstants.UPPER_LEFT_CORNER, headerTable.getTableHeader());
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        scrAgegroup = new javax.swing.JScrollPane();
        tbAgegroup = new javax.swing.JTable();
        scrClasslist = new javax.swing.JScrollPane();
        lsClasslist = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        btnAdd = new javax.swing.JButton();

        setResizable(false);
        setTitle("View Detail Age Group");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        jPanel1.setMinimumSize(new java.awt.Dimension(640, 189));
        jPanel1.setLayout(new java.awt.GridBagLayout());

        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(204, 204, 255)));
        jPanel2.setMinimumSize(new java.awt.Dimension(500, 200));
        jPanel2.setPreferredSize(new java.awt.Dimension(500, 200));
        jPanel2.setLayout(new java.awt.GridBagLayout());

        scrAgegroup.setMinimumSize(new java.awt.Dimension(200, 150));
        scrAgegroup.setPreferredSize(new java.awt.Dimension(200, 150));

        tbAgegroup.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        tbAgegroup.setMinimumSize(new java.awt.Dimension(400, 150));
        tbAgegroup.setPreferredSize(new java.awt.Dimension(400, 150));
        tbAgegroup.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbAgegroupMouseClicked(evt);
            }
        });
        scrAgegroup.setViewportView(tbAgegroup);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 0.8;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(20, 6, 18, 6);
        jPanel2.add(scrAgegroup, gridBagConstraints);

        scrClasslist.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Class", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 14), new java.awt.Color(153, 153, 255))); // NOI18N
        scrClasslist.setMinimumSize(new java.awt.Dimension(80, 150));
        scrClasslist.setPreferredSize(new java.awt.Dimension(80, 150));

        lsClasslist.setMaximumSize(new java.awt.Dimension(50, 100));
        lsClasslist.setMinimumSize(new java.awt.Dimension(50, 100));
        lsClasslist.setPreferredSize(new java.awt.Dimension(50, 100));
        scrClasslist.setViewportView(lsClasslist);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL;
        gridBagConstraints.insets = new java.awt.Insets(20, 6, 18, 6);
        jPanel2.add(scrClasslist, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        jPanel1.add(jPanel2, gridBagConstraints);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18));
        jLabel1.setText("View Detail Age Group");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 10, 10);
        jPanel1.add(jLabel1, gridBagConstraints);

        jPanel3.setLayout(new java.awt.GridBagLayout());

        btnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/aptech/childcare/icon/accept.png"))); // NOI18N
        btnAdd.setText("Add");
        btnAdd.setMaximumSize(new java.awt.Dimension(85, 25));
        btnAdd.setMinimumSize(new java.awt.Dimension(85, 25));
        btnAdd.setPreferredSize(new java.awt.Dimension(85, 25));
        btnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(4, 4, 4, 4);
        jPanel3.add(btnAdd, gridBagConstraints);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(20, 10, 22, 10);
        jPanel1.add(jPanel3, gridBagConstraints);

        add(jPanel1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog

    private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddActionPerformed
        // TODO add your handling code here:
        int n = tbAgegroup.getSelectedRow();
        if (n == -1) {
            JOptionPane.showMessageDialog(this, "Choose Agegroup to View");
            return;
        }
        index = tbAgegroup.getSelectedRow();
        AgeID = age.get(index).getAgeGroupID();
        frm.txtSetAgegroupIDOfClass.setText(String.valueOf(AgeID));

        this.dispose();

    }//GEN-LAST:event_btnAddActionPerformed

    private void tbAgegroupMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbAgegroupMouseClicked
        try {
            index = tbAgegroup.getSelectedRow();
            Vector v = new Vector();
            int AgeID1 = age.get(index).getAgeGroupID();
            v.add("" + buz.findByIDAge(AgeID1).getAgeGroup());
            lsClasslist = new JList(v);
            scrClasslist.setViewportView(lsClasslist);
            lsClasslist.updateUI();
        } catch (IOException ex) {
            Logger.getLogger(ViewAgegroupDlg.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_tbAgegroupMouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    ViewAgegroupDlg dialog = new ViewAgegroupDlg(new MainFrm(), true);
                    dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                        @Override
                        public void windowClosing(java.awt.event.WindowEvent e) {
                            System.exit(0);
                        }
                    });
                    dialog.setVisible(true);
                } catch (IOException ex) {
                    Logger.getLogger(ViewAgegroupDlg.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JList lsClasslist;
    private javax.swing.JScrollPane scrAgegroup;
    private javax.swing.JScrollPane scrClasslist;
    private javax.swing.JTable tbAgegroup;
    // End of variables declaration//GEN-END:variables
}
